/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductDetails2.java
 *
 * Created on Dec 10, 2009, 11:04:15 AM
 */

package GUI;

import Business.Product_BS;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;

/**
 *
 * @author trangnn_b00557
 */
public class ProductDetails extends javax.swing.JDialog {
    Product_BS product;

    /** Creates new form ProductDetails2 */
    public ProductDetails(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void setProduct(Product_BS product) {
        this.product = product;
        lblcode.setText(String.valueOf(product.getpCode()));
        txtName.setText(product.getpName());
        txtType.setText(product.getpType());
        txtProductionDate.setText(product.getpProductionDate());
        txtExpiryDate.setText(product.getpExpiryDate());
        txtImportDate.setText(product.getpImportDate());
        txtPrice.setText(String.valueOf(product.getpPrice()));
        txtTax.setText(String.valueOf(product.getpTax()));
        txtQuantity.setText(String.valueOf(product.getpQuantity()));
        txtSellPrice.setText(String.valueOf(product.getpSellPrice()));
        txtDescription.setText(product.getpDescription());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblType = new javax.swing.JLabel();
        lblDecription = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        btExit = new javax.swing.JButton();
        txtExpiryDate = new javax.swing.JTextField();
        lblCodeaaa = new javax.swing.JLabel();
        lbl = new javax.swing.JLabel();
        lblcode = new javax.swing.JLabel();
        txtImportDate = new javax.swing.JTextField();
        btUpdate = new javax.swing.JButton();
        lblPrice = new javax.swing.JLabel();
        lblProductionDate = new javax.swing.JLabel();
        txtType = new javax.swing.JTextField();
        lblExpiryDate = new javax.swing.JLabel();
        txtProductionDate = new javax.swing.JTextField();
        txtQuantity = new javax.swing.JTextField();
        lblPrice1 = new javax.swing.JLabel();
        lblPrice2 = new javax.swing.JLabel();
        txtTax = new javax.swing.JTextField();
        lblPrice3 = new javax.swing.JLabel();
        txtSellPrice = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Product Details", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Monotype Corsiva", 1, 36), new java.awt.Color(51, 102, 0))); // NOI18N

        lblType.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblType.setText("Type : ");

        lblDecription.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblDecription.setText("Description : ");

        lblName.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblName.setText("Name : ");

        btExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Close-icon.png"))); // NOI18N
        btExit.setText("Close");
        btExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExitActionPerformed(evt);
            }
        });

        lblCodeaaa.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblCodeaaa.setText("Code : ");

        lbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbl.setText("Import Date : ");

        lblcode.setText("code");

        btUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Edit.png"))); // NOI18N
        btUpdate.setText("Update");
        btUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUpdateActionPerformed(evt);
            }
        });

        lblPrice.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice.setText(" Price : ");

        lblProductionDate.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblProductionDate.setText("Production Date : ");

        lblExpiryDate.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblExpiryDate.setText("Expiry Date : ");

        lblPrice1.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice1.setText("Quantity:");

        lblPrice2.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice2.setText("Tax:");

        lblPrice3.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice3.setText("Sell Price:");

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        jScrollPane1.setViewportView(txtDescription);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(54, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btUpdate)
                        .addGap(55, 55, 55)
                        .addComponent(btExit)
                        .addGap(23, 23, 23))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblPrice1)
                            .addComponent(lblPrice2)
                            .addComponent(lblPrice3)
                            .addComponent(lblDecription))
                        .addGap(226, 226, 226)))
                .addGap(124, 124, 124))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addComponent(lblExpiryDate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtQuantity, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtType)
                    .addComponent(txtProductionDate, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                    .addComponent(txtExpiryDate, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtImportDate, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtPrice, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtName, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTax, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSellPrice, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblcode, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(122, 122, 122))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(33, 33, 33)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(lblName)
                        .addComponent(lblCodeaaa)
                        .addComponent(lblType)
                        .addComponent(lblProductionDate)
                        .addComponent(lbl)
                        .addComponent(lblPrice))
                    .addGap(231, 231, 231)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(lblcode)
                .addGap(18, 18, 18)
                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtProductionDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtExpiryDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtImportDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblExpiryDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPrice2))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(13, 13, 13)
                        .addComponent(txtSellPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(128, 128, 128)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btUpdate)
                            .addComponent(btExit, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblPrice1)
                        .addGap(18, 18, 18)
                        .addComponent(lblPrice3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblDecription, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(23, 23, 23))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(8, 8, 8)
                    .addComponent(lblCodeaaa)
                    .addGap(18, 18, 18)
                    .addComponent(lblName)
                    .addGap(24, 24, 24)
                    .addComponent(lblType)
                    .addGap(18, 18, 18)
                    .addComponent(lblProductionDate)
                    .addGap(62, 62, 62)
                    .addComponent(lbl)
                    .addGap(24, 24, 24)
                    .addComponent(lblPrice)
                    .addContainerGap(378, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUpdateActionPerformed
        // TODO add your handling code here:
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (txtType.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Please enter Type");
            return;
        }
        if (txtProductionDate.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter date");
            return;
        }
        if (txtProductionDate.getText().trim().length() != dateFormat.toPattern().length()){
            JOptionPane.showMessageDialog(this, "Please enter correct date format YYYY/MM/DD");
            return;
        }

        if (txtExpiryDate.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter date");
            return;
        }
        if (txtExpiryDate.getText().trim().length() != dateFormat.toPattern().length()){
            JOptionPane.showMessageDialog(this, "Please enter correct date format YYYY/MM/DD");
            return;
        }

        if (txtImportDate.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter date");
            return;
        }
        if (txtImportDate.getText().trim().length() != dateFormat.toPattern().length()){
            JOptionPane.showMessageDialog(this, "Please enter correct date format YYYY/MM/DD");
            return;
        }
        if (txtName.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter name");
            return;
        }
        if (txtProductionDate.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter Type");
            return;
        }
        if (txtPrice.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter price");
            return;
        }
        if (txtTax.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter tax");
            return;
        }
        if (txtQuantity.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter quantity");
            return;
        }
        if (txtSellPrice.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this,"Please enter sell price");
            return;
        }
        product.setpName(txtName.getText());
        product.setpType(txtType.getText());
        product.setpProductionDate(txtProductionDate.getText());
        product.setpExpiryDate(txtExpiryDate.getText());
        product.setpImportDate(txtImportDate.getText());
        product.setpPrice(Float.parseFloat(txtPrice.getText()));
        product.setpTax(Float.parseFloat(txtTax.getText()));
        product.setpQuantity(Integer.parseInt(txtQuantity.getText()));
        product.setpSellPrice(Float.parseFloat(txtSellPrice.getText()));
        product.setpDescription(txtDescription.getText());
        if(!DAO.Product_DAO.update(product)) {
            JOptionPane.showMessageDialog(this, "Cannot update this product");
            return;
        }

        this.dispose();
    }//GEN-LAST:event_btUpdateActionPerformed

    private void btExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExitActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btExitActionPerformed

    /**
    * @param args the command line arguments
    */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btExit;
    private javax.swing.JButton btUpdate;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbl;
    private javax.swing.JLabel lblCodeaaa;
    private javax.swing.JLabel lblDecription;
    private javax.swing.JLabel lblExpiryDate;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblPrice1;
    private javax.swing.JLabel lblPrice2;
    private javax.swing.JLabel lblPrice3;
    private javax.swing.JLabel lblProductionDate;
    private javax.swing.JLabel lblType;
    private javax.swing.JLabel lblcode;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtExpiryDate;
    private javax.swing.JTextField txtImportDate;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtProductionDate;
    private javax.swing.JTextField txtQuantity;
    private javax.swing.JTextField txtSellPrice;
    private javax.swing.JTextField txtTax;
    private javax.swing.JTextField txtType;
    // End of variables declaration//GEN-END:variables

}
